package com.md.youyouhui.ui.home;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityAddressBinding;
import com.md.youyouhui.net.response.GetAddressResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityPicker;

/**
 * Created by AdministratorZJ
 * on 2019/4/29.
 */
public class AddressActivity extends BaseActivity {
    private ActivityAddressBinding addressBinding;
    private MainViewModel viewModel;
    private String tag = "0";
    private String id;

    @Override
    public void setContentLayout() {
        addressBinding = DataBindingUtil.setContentView(this, R.layout.activity_address);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(addressBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        addressBinding.addressAreaText.setOnClickListener(v -> showPicker());
        addressBinding.addressBtn.setOnClickListener(v -> addArea());
        addressBinding.addressCheck.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                tag = "1";
            } else {
                tag = "0";
            }
        });
        int size = getIntent().getIntExtra("size", -1);
        if (size == 0) {
            addressBinding.addressCheck.setChecked(true);
        } else {
            addressBinding.addressCheck.setChecked(false);
        }
        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            GetAddressResponse address = (GetAddressResponse) extras.getSerializable("address");
            if (null == address) {
                return;
            }
            id = address.id;
            setAddress(address);
        }
    }

    private void setAddress(GetAddressResponse address) {
        addressBinding.addressPeopleText.setText(address.consignee);
        addressBinding.addressPhoneText.setText(address.mobilePhone);
        addressBinding.addressAreaText.setText(address.area);
        addressBinding.addressMinuteText.setText(address.detailedAddress);
        addressBinding.addressCodeText.setText(address.postcode);
        if ("0".equals(address.tag)) {
            addressBinding.addressCheck.setChecked(false);
        } else {
            addressBinding.addressCheck.setChecked(true);
        }
    }

    private void addArea() {
        String name = addressBinding.addressPeopleText.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(ct, "请输入收货人姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = addressBinding.addressPhoneText.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(ct, "请输入手机号码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (phone.length() < 6) {
            Toast.makeText(ct, "手机号码最少6位", Toast.LENGTH_SHORT).show();
            return;
        }
        String area = addressBinding.addressAreaText.getText().toString();
        if (TextUtils.isEmpty(area)) {
            Toast.makeText(ct, "请选择选择地区", Toast.LENGTH_SHORT).show();
            return;
        }
        String minute = addressBinding.addressMinuteText.getText().toString();
        if (TextUtils.isEmpty(minute)) {
            Toast.makeText(ct, "请输入详细地址", Toast.LENGTH_SHORT).show();
            return;
        }
        String code = addressBinding.addressCodeText.getText().toString();
        /*if (TextUtils.isEmpty(code)) {
            Toast.makeText(ct, "请输入邮政编码", Toast.LENGTH_SHORT).show();
            return;
        }*/
        viewModel.addAddress(id, name, phone, area, minute, code, tag).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    addressBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    addressBinding.progress.setVisibility(View.GONE);
                    Toast.makeText(ct, "保存成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case ERROR:
                    addressBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
        /*Intent intent = new Intent();
        intent.putExtra("name", name);
        intent.putExtra("phone", phone);
        intent.putExtra("area", area);
        intent.putExtra("minute", minute);
        intent.putExtra("code", code);
        setResult(RESULT_OK, intent);
        finish();*/
    }

    private void showPicker() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().peekDecorView().getWindowToken(), 0);
        JDCityPicker cityPicker = new JDCityPicker();
        cityPicker.init(this);
        cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                addressBinding.addressAreaText.setText(String.format("%s%s%s", province.getName(), city.getName(), district.getName()));
            }

            @Override
            public void onCancel() {
            }
        });
        cityPicker.showCityPicker();
    }
}
